# -*- coding: utf-8 -*-
from pathlib import Path
import time
from re_utils import re_anonymize_need, ReType, scan_files, dir_path, bak_dir_path, exts

def anonymize_file(file_path, bak_dir_file_path):
    with open(bak_dir_file_path, 'r', encoding='utf-8') as infile, open(file_path, 'w', encoding='utf-8') as outfile:
        for line in infile:
            re_text = re_anonymize_need(line)
            outfile.write(re_text)
    pass


if __name__ == '__main__':
    extensions = [ext.strip() for ext in exts.split(',')] if exts else None
    print(extensions)
    try:
        for idx, file in enumerate(scan_files(dir_path, extensions, max_depth=5), 1):
            try:
                time1 = time.time()
                print(f"{idx}. {file}")
                bak_dir_file_path = str(file).replace(dir_path, bak_dir_path)
                print(f"{idx}. {bak_dir_file_path}")
                anonymize_file(file, bak_dir_file_path)
                print(f"脱敏成功, 耗时：", (time.time() - time1))
            except Exception as e:
                print(f"脱敏失败错误: {e}")
    except Exception as e:
        print(f"错误: {e}")
